package cn.linlane.service.sys;

import cn.linlane.dao.sys.SysDepartDao;
import cn.linlane.dao.sys.SysUserDao;
import cn.linlane.entity.sys.SysDepart;
import cn.linlane.entity.sys.SysUser;
import cn.linlane.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


@Service
public class SysDepartService extends BaseService<SysDepartDao, SysDepart> {

    @Autowired
    private ShiroService shiroService;

    @Autowired
    private SysUserDao sysUserDao;

    public Boolean existsByStoreCode(String storeCode){
        return baseDao.existsAllByStoreCode(storeCode);
    }

    /**
     * 根据Id查询组织机构
     *
     * @param id
     * @return
     */
    public SysDepart findSysDepartsById(Long id) throws Exception {
        return baseDao.findByParentId(id);
    }

    public void deleteDataPermition(Long id){
        baseDao.deleteDataPermition(id);
    }

    public void updateDataPermition(Long id, String storeCode){
        baseDao.insertDataPermition(id, storeCode);
    }

    /**
     * 获取当前用户门店树
     * @return
     */
    public List<SysDepart> getDepartTree(){

        SysUser sysUser = shiroService.getSysUser();

        Set<String> storeCodes = baseDao.findAllDatePermition(sysUser.getId());

        List<SysDepart> sysDepartList = baseDao.findAllByStoreCodeIn(storeCodes);

        //返回前端树
        List<SysDepart> frontDeparts = new ArrayList<>();

        for (SysDepart sysDepart : sysDepartList){
            if (sysDepart.getParentId() == null){
                frontDeparts.add(sysDepart);
            }
        }

        getChild(frontDeparts, sysDepartList);

        return frontDeparts;
    }

    /**
     * 获取全部部门的树
     * @return
     */
    public List<SysDepart> getAllDepart(){

        List<SysDepart> sysDepartList = baseDao.findAll();

        //返回前端树
        List<SysDepart> frontDeparts = new ArrayList<>();

        for (SysDepart sysDepart : sysDepartList){
            if (sysDepart.getParentId() == null){
                frontDeparts.add(sysDepart);
            }
        }
        getChild(frontDeparts, sysDepartList);
        return frontDeparts;
    }

    /**
     * 递归
     * @return
     */
    public List<SysDepart> getChild(List<SysDepart> frontDeparts, List<SysDepart> sysDeparts){

        if (frontDeparts.size() == 0){
            return null;
        }else {
            List<SysDepart> departs = new ArrayList<>();
            for (SysDepart sysDepart : frontDeparts){
                List<SysDepart> sysDepartList = new ArrayList<>();
                for (SysDepart sysDepart1 : sysDeparts){
                    if (sysDepart.getId().equals(sysDepart1.getParentId())){
                        sysDepartList.add(sysDepart1);
                    }
                }
                if (sysDepartList.size() > 0){
                    sysDepart.setSysDeparts(sysDepartList);
                }
                departs.addAll(sysDepartList);
            }
            return getChild(departs, sysDeparts);
        }
    }


    /**
     * 根据id和storename查询
     */
    public Page<SysDepart> getDeparts(Integer parentId, String searchKey, Integer pageNum, Integer pageSize){
        return baseDao.fetchAllByParentId(parentId, searchKey, PageRequest.of(pageNum, pageSize));
    }

    /**
     * departCode查询状态
     *
     * @param storeCode
     * @return
     */
    public List<SysDepart> findByDepartCode(String storeCode){
        return baseDao.findByStoreCode(storeCode);
    }

    /**
     * 查看门店是否可以下发卡
     * @return
     */
    public SysDepart findDistributeStores(String storeCode){
        return baseDao.findDistributeStores(storeCode);
    }
}
